package com.sky.service.user.impl;

import com.sky.context.BaseContext;
import com.sky.mapper.user.AddressBookMapper;
import com.sky.pojo.AddressBook;
import com.sky.service.user.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService{
    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 查询当前登录用户的所有地址信息
     */
    @Override
    public List<AddressBook> queryAll() {
        Long userId = BaseContext.getCurrentId();
        return addressBookMapper.queryAll(userId);
    }

    /**
     * 查询默认地址
     */
    @Override
    public AddressBook queryDefault() {
//        Long userId = BaseContext.getCurrentId();
//        return addressBookMapper.queryDefault(userId);
        AddressBook addressBook = AddressBook.builder()
                .userId(BaseContext.getCurrentId())
                .isDefault(1)
                .build();
        return addressBookMapper.query(addressBook);
    }
    /**
     * 根据id查询地址
     */
    @Override
    public AddressBook queryById(Long id) {
        AddressBook addressBook = AddressBook.builder()
                .userId(BaseContext.getCurrentId())
                .id(id).build();
        return addressBookMapper.query(addressBook);
    }
    /**
     * 新增地址
     */
    @Override
    public void add(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.insert(addressBook);
    }
    /**
     * 根据id删除地址
     */
    @Override
    public void delete(Long id) {
        addressBookMapper.delete(id);
    }
    /**
     * 设置默认地址
     */
    @Override
    public void setDefault(AddressBook addressBook) {
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }
    /**
     * 根据id修改地址
     */
    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }
}
